<!DOCTYPE html>
<html lang="en">
<head>
    <!-- 剩余问题：
        1.设置各商品的初始数量为 0，但计算总价时，某个商品减为 <0 时，总价计算会有负数
    -->
    <!-- 使用计算属性 -->
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>简单商品清单操作练习2.0</title>
    <style type="text/css">
        *{
            padding: 5px;
            text-align: center;
        }
        #app table, td, th{
            border: 1px solid black;
            border-collapse: collapse;
        }
    </style>
</head>
<body>

    <div id="app">
        <table>
            <tr>
                <th>商品编号</th>
                <th>商品名称</th>
                <th>商品单价</th>
                <th>商品数量</th>
                <th>商品总价</th>
                <th>商品操作</th>
            </tr>
            <!-- 购物车为空时 -->
            <!-- 此处为什么item可以直接使用 -->
            <tr v-if="commodityInfo.every(item => item.nowNumber == 0 )">
                <td colspan="6">购物车为空</td>
            </tr>
            <template v-for="(item,index) in filterCommodityInfo">
                <tr v-if="item.nowNumber>0" :key="index">
                    <td>{{item.commdityId}}</td>
                    <td>{{item.commodityName}}</td>
                    <td>{{item.price}}</td>
                    <td>
                        <button @click="item.nowNumber--">&minus;</button>
                        <span>{{item.nowNumber}}</span>
                        <button @click="item.nowNumber++">+</button>
                    </td>
                    <td>{{item.price*item.nowNumber}}</td>
                    <td>
                        <!-- 删除数组数据 -->
                        <button @click="item.nowNumber=0">删除</button>
                    </td>
                </tr>
            </template>
            
        </table>

        <br />
        <table>
            <tr>
                <th>总价</th>
                <th>
                    {{totalPrice}}
                    <!-- 使用下面的判断会使得计算结果出问题 -->
                    <!-- <span v-if="totalPrice >= 0">{{totalPrice}}</span>
                    <span v-else>0</span> -->
                </th>
            </tr>
        </table>
    </div>

    <script src="vue.js"></script>
    <script>
        new Vue({
            el:"#app",
            data:function(){
                return {
                    // 商品 commodity
                    commodityInfo:[
                        { commdityId:1, commodityName:"苹果", price:10, nowNumber:1,},
                        { commdityId:2, commodityName:"香蕉", price:20, nowNumber:1,},
                        { commdityId:3, commodityName:"栗子", price:30, nowNumber:1,},
                        { commdityId:4, commodityName:"李子", price:40, nowNumber:1,}
                    ],
                    s:100,
                    a:200
                }
            },
            computed:{
                // 新数组方法filter
                filterCommodityInfo:function(){
                    return this.commodityInfo.filter(item => item.nowNumber > 0)
                },
                // 新数组方法reduce 新思维方法 
                totalPrice:function(){
                    return this.commodityInfo.reduce((sum,item) => {
                        return sum + item.price * item.nowNumber},0)
                }
            },

            methods:{
                
            }
        })
    </script>
</body>
</html>